package memleak.test.com.testlist;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;


import com.gzgd.log.LogUtils;

import java.util.List;

/**
 * Created by runrun on 2017/4/25.
 */

public class LeftLayout extends RelativeLayout {
    private static final String TAG = "LeftLayout";

    private int mPrePagePos;//上一次播放的分类列表
    private int mPreChannelPos;//上一次播放的频道位置
    private int mPagePos;
    private int mChannelPos;
    private ImageView mChannelListBg;
    private TabViewPager mTabView;
    private LabelView mLabelView;
    private ImageView mAdView;
    private PreferencesDataSource mPreferencesDataSource;

    /**
     * callback happend here while viewpager scroller
     */
    private OnTabChannelListener mOnTabChannelListener = new OnTabChannelListener() {

        @Override
        public void onTabSelected(ChannelCategory channelCategory) {
            if (null == channelCategory) {
                return;
            }
            mLabelView.updateView(channelCategory.getLabelBean());
        }
    };
    private TabViewPager.OnPageLayoutListener mOnPageLayoutListener = new TabViewPager.OnPageLayoutListener() {
        @Override
        public void onLayoutOver() {
            if (View.VISIBLE != getVisibility()) {
                return;
            }
            if (View.GONE == mChannelListBg.getVisibility()) {
                mChannelListBg.setVisibility(View.VISIBLE);
            }
            if (View.GONE == mLabelView.getVisibility()) {
                mLabelView.setVisibility(View.VISIBLE);
            }
            if (View.GONE == mLabelView.getVisibility()) {
                mLabelView.setVisibility(View.VISIBLE);
            }
            if (View.GONE == mAdView.getVisibility()) {
                mAdView.setVisibility(View.VISIBLE);
            }
        }
    };

    public LeftLayout(Context context) {
        this(context, null);
    }

    public LeftLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LeftLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initData();
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.left_layout, this, true);
        mChannelListBg = (ImageView) findViewById(R.id.channelListBg);
        mLabelView = (LabelView) findViewById(R.id.labelView);
        mTabView = (TabViewPager) findViewById(R.id.tabView);
        mAdView = (ImageView) findViewById(R.id.adView);

        mChannelListBg.setVisibility(GONE);
        mLabelView.setVisibility(GONE);
        mAdView.setVisibility(GONE);
    }

    private void initData() {
        mTabView.setOnPageLayoutListener(mOnPageLayoutListener);
        mTabView.setTabChannelListener(mOnTabChannelListener);  //tab 切换回调
        mPreferencesDataSource = PreferencesDataSource.newInstance();
        mPagePos = mPreferencesDataSource.getPagePos();
        mChannelPos = mPreferencesDataSource.getChannelPos();
        mPrePagePos = mPagePos;
        mPreChannelPos = mChannelPos;
        LogUtils.d(TAG, "initData mPagePos:" + mPagePos + " mChannelPos:" + mChannelPos);
    }

    public void switchChannelByNumber(int logicNum) {
        mTabView.selectChannelByNumber(logicNum);
    }

    public LiveChannel getChannelByNumber(int logicNum) {

        return mTabView.getChannelByNumber(logicNum);
    }

    public boolean hide() {
        boolean isHandle = false;
        if (View.VISIBLE == getVisibility()) {
            setVisibility(View.INVISIBLE);
            isHandle = true;
        }
        return isHandle;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    public void updateView(List<ChannelCategory> channelCategories) {
        mTabView.updateView(channelCategories);
    }

    public void updateView(List<ChannelCategory> channelCategories, boolean isRefresh) {
        mTabView.updateView(channelCategories);
        if(isRefresh){
            Log.i(TAG,">>>>>>>>>>>>>>updateView");
            LiveApplication app = LiveApplication.getGlobalContext();
            app.setFlagUpdateData(true);
            mTabView.getAdapter().notifyDataSetChanged();
        }
    }

    @Override
    public void setVisibility(int visibility) {
        LogUtils.d(TAG, "setVisibility:" + visibility);
        if (visibility == GONE) {
            mTabView.setCurrentItem(mPagePos);
        } else if (visibility == VISIBLE) {
            LogUtils.d(TAG, "pagePos:" + mPagePos);
            LogUtils.d(TAG, "channelPos:" + mChannelPos);
            turnPage(mPagePos);
            mTabView.showCurrentChannel(mChannelPos);
            //如果缓存了喜爱页面显示的时候刷新,如果没有缓存或者当前就在喜爱页面不刷新
            isCacheFavoritePage();
        }
        super.setVisibility(visibility);
    }

    private void isCacheFavoritePage() {
        if ((mTabView.getPreCategoryId() != null
                && mTabView.getPreCategoryId().equals(Constant.FAVORITE_CATEGORY)) ||
                (mTabView.getNextCategoryId() != null &&
                        mTabView.getNextCategoryId().equals(Constant.FAVORITE_CATEGORY))) {
            mTabView.getAdapter().notifyDataSetChanged();
        }
    }

    public int getCurPagePos() {
        return mTabView.getCurrentItem();
    }

    public void turnPage(int pos) {
        if (pos >= mTabView.getPagesCounts() || pos < 0) {
            LogUtils.i(TAG, "mTabView.getPagesCounts(): " + mTabView.getPagesCounts());
            LogUtils.e(TAG, "page pos error: " + pos);
            return;
        }
        mTabView.setCurrentItem(pos);
    }


    public List<LiveChannel> getDataSet(int pagePos) {
        return ((TabAdapter) mTabView.getAdapter()).getDatas(pagePos);
    }

    /**
     * 上、下鍵切臺
     *
     * @param direction
     */
    public VideoBean switchChannelByDirection(int direction) {
        List<LiveChannel> channels = getDataSet(mPagePos);
        if (channels == null) {
            return null;
        }
        int size = channels.size();
        if (0 == size) {
            return null;
        }
        int nextPos;
        int change = direction == View.FOCUS_FORWARD ? 1 : -1;
        nextPos = mChannelPos + change;

        if (nextPos >= size) {
            nextPos = 0;
        }
        if (nextPos < 0) {
            nextPos = size - 1;
        }
        LogUtils.d(TAG, "mChannelPos:" + nextPos);
        ((LiveApplication) getContext().getApplicationContext()).changeChannelPos(nextPos);
        VideoBean bean = TransformBeanTool.transforVideoBean(channels.get(nextPos));
        PreferencesDataSource.newInstance().savePreChannelNum(TransformUtils.parseInt(bean.getChannelNum(), 999));
        LogUtils.d(TAG, "next videoBean:" + bean + " pos:" + nextPos);
        return bean;
        //((LiveApplication) getContext().getApplicationContext()).adjustPlay(bean);
    }

    /**
     * 更新单个滑页面内容
     *
     * @param channelCategory
     */
    public void updateSinglePage(ChannelCategory channelCategory) {
        mTabView.updateSinglePage(channelCategory);
    }

    /**
     * 更新喜爱列表
     *
     * @param channel
     */
    public void updateFavoriteList(LiveChannel channel) {
        mTabView.updateFavoritesOnPagerAdapter(channel);
    }

    public int getPrePagePos() {
        return mPrePagePos;
    }

    public void setPrePagePos(int pagePos) {
        LogUtils.i(TAG, "real setPrePagePos to " + pagePos + "; old value is " + mPrePagePos);
        mPrePagePos = pagePos;
    }

    public void setPreChannelPos(int channelPos) {
        mPreChannelPos = channelPos;
    }

    public int getPreChannelPos() {
        return mPreChannelPos;
    }

    public int getPagePos() {
        return mPagePos;
    }

    public void setPagePos(int pagePos) {
        LogUtils.i(TAG, "setPagePos pagePos is " + pagePos + "; mPagePos is " + mPagePos + "; mPrePagePos is " + mPrePagePos);

        if (LiveApplication.getGlobalContext().isForeground()) {//排除不在前台播放接受关机频道时会改pagePos的情况
            LogUtils.i(TAG, "real setPagePos pagePos is " + pagePos + "; mPrePagePos will " + mPagePos);
            mPrePagePos = mPagePos;//记录前一次播放页
        }
        mPagePos = pagePos;
        mPreferencesDataSource.savePagePos(pagePos);
    }

    public int getChannelPos() {
        return mChannelPos;
    }

    public void setChannelPos(int channelPos) {

        LiveApplication app = LiveApplication.getGlobalContext();
        if (!app.isSwitchChannel() && LiveApplication.getGlobalContext().isForeground()) {
            LogUtils.i(TAG, "setChannelPos channelPos is " + channelPos + "; mChannelPos is " + mChannelPos + "; mPreChannelPos is " + mPreChannelPos);
            mPreChannelPos = mChannelPos;//记录前一次播放位置,若是长按则不记录
        }
        mChannelPos = channelPos;
        mPreferencesDataSource.saveChannelPos(channelPos);
    }

    public int getPagePosbyId(String s) {
        return mTabView.findPagebyId(s);
    }


    public String getCurCategoryId() {
        return mTabView.getCurCategoryId();
    }

    public String getPreCategoryId() {
        return mTabView.getPreCategoryId();
    }

    public String getNextCategoryId() {
        return mTabView.getNextCategoryId();
    }
}
